package com.demo.villageresidence.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.villageresidence.dto.Result;
import com.demo.villageresidence.pojo.Tender;
import com.demo.villageresidence.pojo.User;
import com.demo.villageresidence.pojo.VehicleApply;
import com.demo.villageresidence.service.UserService;
import com.demo.villageresidence.service.VehicleApplyService;
import com.demo.villageresidence.service.VehicleApprovalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/vehicleApply")
public class VehicleApplyController {

    @Autowired
    private VehicleApplyService vehicleApplyService;

    @Autowired
    private VehicleApprovalService vehicleApprovalService;

    @Autowired
    private UserService userService;


    @GetMapping("getVehicleApplyByPageNoPass")
    public Result getVehicleApplyByPageNoPass(@RequestParam(value = "current", defaultValue = "1") Integer current,
                                              @RequestParam(value = "pageSize", defaultValue = "10")  Integer pageSize){
        //  2为待审核
        Page<VehicleApply> page = vehicleApplyService.query().eq("ve_status",2)
                .page(new Page<>(current, pageSize));
        page.getRecords().forEach(vehicleApply -> {
            User user = userService.getById(vehicleApply.getUserId());
            vehicleApply.setUserPhone(user.getUserPhone());
        });
        // 获取当前页数据
        List<VehicleApply> records = page.getRecords();
        // 获取总条数
        long total = page.getTotal();
        return Result.ok(records,total);

    }

    @GetMapping("getVehicleApplyById/{id}")
    public Result getVehicleApplyById(@PathVariable("id") Integer id){
        VehicleApply vehicleApply = vehicleApplyService.getById(id);
        return Result.ok(vehicleApply);
    }

    @GetMapping("getAllVehicleApplyByUserId/{userId}")
    public Result getVehicleApplyByUserId(@PathVariable("userId") Integer userId){

        List<VehicleApply> vehicleApplyList = vehicleApplyService.query().eq("user_id",userId).list();
        vehicleApplyList.forEach(vehicleApply -> {
            User user = userService.getById(vehicleApply.getUserId());
            vehicleApply.setUserPhone(user.getUserPhone());
        });
        return Result.ok(vehicleApplyList);
    }

    @PostMapping("addVehicleApply")
    public Result addVehicleApply(@RequestBody VehicleApply vehicleApply){
        vehicleApply.setVeTime(LocalDateTime.now());
        vehicleApply.setIsDelete(0);
        vehicleApply.setVeStatus(2);
        if (vehicleApplyService.save(vehicleApply)){
            return Result.ok("申请成功");
        }else{
            return Result.fail("申请失败");
        }
    }

    @GetMapping("getVehicleApplyNum")
    public Result getVehicleApplyNum(){
        Integer veStatus = vehicleApplyService.query().eq("ve_status", 2).count();
        System.out.println(veStatus);
        if (veStatus>0) {
            return Result.ok(veStatus);
        }else {
            return Result.ok(0);
        }

    }

}
